// 2023-03-14 10:58:25
/// <reference path="../../types/express.d.ts" />
import * as service from '../../service/service.js'
import { CreateToken, CheckToken } from '../../util/Token.js';
import { request } from 'express'
import { response } from 'express'
import CODE from '../../config/data.config.js';
class BaseController {
    /**
     * @param {Function} next 
     * @param {request} req 
     * @param {response} res 
     */
    static async CheckToken(req, res, next) {
        try {
            const authorization = req.headers['authorization']
            if (authorization && authorization.length) {
                const token = authorization.split(' ')[1];
                let { status } = await CheckToken(token)
                if (status) {
                    res.send({
                        msg: 'token验证成功',
                        code: 1
                    }).end()
                } else
                    res.status(401).send({
                        msg: 'token失效',
                        code: 401,
                        redirectName: '/login'
                    }).end()
            }
            else {
                res.status(401).send({
                    msg: '没有token',
                    code: CODE.CODE_TOKEN,
                    redirectName: '/login'
                })
            }
        } catch (error) {
            next(error)
        }
    }

    /**
     * post 请求
     * @param {Function} next 
     * @param {request} req 
     * @param {response} res 
     */
    static async IsAdmin(req, res, next) {
        try {
            const { phone } = req.query;
            const status = await service.UserService.IsExist(phone, true)
            if (status) {
                res.send({
                    msg: '是管理员',
                    code: CODE.CODE_OK,
                    data: true
                })
            } else {
                res.send({
                    msg: '不是管理员',
                    code: CODE.CODE_FAIL,
                    data: false
                })
            }
        } catch (error) {
            next(error)
        }
    }
}

export default BaseController;